//
//  CommViewController.m
//  YouGoWeGo
//
//  Created by LiuFei on 14/11/19.
//  Copyright (c) 2014年 LiuFei. All rights reserved.
//

#import "CommViewController.h"

#import "CustomTravTableCell.h"
#import "UIImageView+WebCache.h"

@interface CommViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation CommViewController

- (void)dealloc
{
    [_model release];
    [_ID release];
    [_trip_id release];
    [_commArr release];
    [_recommArr release];
    [_commTable release];
    [_image release];
    [_picUrl release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        
        self.commArr = [NSMutableArray array];
        self.recommArr = [NSMutableArray array];
        self.count_1 = 1;
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.navigationItem setTitle:@"评论"];
    
    [self startRequest];
    [self createLeftButton];
    [self createImageView];
    [self createTable];
}

- (void)startRequest
{
    
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.breadtrip.com/trips/%@/waypoints/%@/replies/?count=32767", self.trip_id, self.ID]];
    
    NSURLRequest *requsest = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:requsest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        self.recommArr = [dic objectForKey:@"recommenders"];
        self.commArr = [dic objectForKey:@"comments"];
        
        [_commTable reloadData];
        
    }];
    
}

#pragma mark - 创建左按钮
- (void)createLeftButton
{
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickBackAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    [button release];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}


#pragma mark - 创建UIImageView
- (void)createImageView
{
    self.image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, self.view.frame.size.width - 40, (self.view.frame.size.height - 64) / 3)];
    
    [_image setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", _picUrl]]];
    
    [self.view addSubview:_image];
    [_image release];
}

#pragma mark - 创建tableview
- (void)createTable
{
    self.commTable = [[UITableView alloc] initWithFrame:CGRectMake(0, _image.frame.size.height + 40, self.view.frame.size.width, self.view.frame.size.height - _image.frame.size.height - 40) style:UITableViewStylePlain];
    
    [_commTable setDelegate:self];
    [_commTable setDataSource:self];
    
    _commTable.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [_commTable setRowHeight:120];
    [self.view addSubview:_commTable];
    [_commTable release];
}

#pragma mark - numberOfRowsInSection
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_recommArr count];
}

#pragma mark - cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
    static NSString *cellID = @"commPool";
    CustomTravTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[[CustomTravTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
    }
    
    NSMutableDictionary *recommDic = [_recommArr objectAtIndex:indexPath.row];
    NSMutableDictionary *commDic = [_commArr objectAtIndex:indexPath.row];
    
    cell.nameLabel.text = [recommDic objectForKey:@"name"];
    cell.detailLabel.text = [commDic objectForKey:@"comment"];
    
//    cell.detailLabel.text = @"花开花落，其实应该与季节无关吧。很多时候都是迷茫于自己的生活，找不到更好的理由来让自己不去想些别人认为无关紧要的事情。";
    return cell;
}


#pragma mark - didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}



#pragma mark - 点击事件
- (void)clickBackAction:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 图片根据tableview向上滚动

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    if (scrollView.contentOffset.y > 0) {
//        
//        [UIView beginAnimations:@"kk" context:nil];
//        [UIView setAnimationDuration:1];
//        
//        [self.image setFrame:CGRectMake(20, - self.image.frame.size.height, self.image.frame.size.width - 40, self.image.frame.size.height)];
//        
//        [self.commTable setFrame:CGRectMake(0, 20, self.commTable.frame.size.width, self.view.frame.size.height)];
//        
//        if (self.commTable.frame.origin.y == 20) {
//            
//            [UIView beginAnimations:@"dd" context:nil];
//            [UIView setAnimationDuration:1];
//            
//            [self.commTable setFrame:CGRectMake(0, 40, self.commTable.frame.size.width, self.view.frame.size.height)];
//            
//            [UIView commitAnimations];
//            [self roll];
//
//        }
//        
//        [UIView commitAnimations];
//        
//    } else {
//        
//        [UIView beginAnimations:@"kk" context:nil];
//        [UIView setAnimationDuration:1];
//        
//        [self.image setFrame:CGRectMake(20, 20, self.view.frame.size.width - 40, (self.view.frame.size.height - 64) / 3)];
//        
//        [self.commTable setFrame:CGRectMake(0, _image.frame.size.height + 40, self.view.frame.size.width, self.view.frame.size.height - _image.frame.size.height - 40)];
//        
//        [UIView commitAnimations];
//        
//    }
//    
//}
//
//- (void)roll
//{
//    
//    [UIView beginAnimations:@"kk" context:nil];
//    [UIView setAnimationDuration:0.5];
//    
//    [self.commTable setFrame:CGRectMake(0, 20, self.commTable.frame.size.width, self.view.frame.size.height)];
//
//    [UIView commitAnimations];
//    
//}


@end
